<template>
  <div>
    <h3>商家列表</h3>
    <br>
    <hr>
    <br>
    <!-- 搜索框 -->
    <el-form :inline="true" :model="searchBusiness" class="demo-form-inline">
      <el-form-item label="商家联系人">
        <el-input v-model="searchBusiness.contact" placeholder="商家联系人" clearable />
      </el-form-item>

      <el-form-item label="商家电话">
        <el-input v-model="searchBusiness.telephone" placeholder="商家电话" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="selectBusiness">搜索</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格标签 -->
    <el-table :data="businessData" style="width: 100%" border stripe>
      <el-table-column type="index" width="100" label="序号" align="center" />
      <el-table-column prop="account" label="商家账号" align="center" />
      <el-table-column prop="contact" label="联系人姓名" align="center" />
      <el-table-column prop="reg_time" label="商家注册时间" align="center" />
      <el-table-column prop="telephone" label="商家电话" align="center" />

      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" @click="openEditDialog(scope.row)">修改</el-button>
          <el-button type="danger" @click="delEmployee(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <!-- 分页工具条 -->
    <el-pagination :current-page="currentPage" :page-size="pageSize" :page-sizes="[2, 10, 20, 50]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />



    <!-- 修改/新增对话框 -->
    <el-dialog v-model="dialogFormVisibleEdit" :title="title" width="500">
      <el-form :model="businessEdit">
        <el-form-item label="商家账号">
          <el-input v-model="businessEdit.account" autocomplete="off" placeholder="请输入商家账号" />
        </el-form-item>

        <el-form-item label="密码">
          <el-input v-model="businessEdit.password" autocomplete="off" type="password" clearable />
        </el-form-item>

        <el-form-item label="联系人姓名">
          <el-input v-model="businessEdit.contact" autocomplete="off" />
        </el-form-item>

        <el-form-item label="商家电话">
          <el-input v-model="businessEdit.telephone" autocomplete="off" />
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
          <el-button type="primary" @click="editOrAddBusiness">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ElMessageBox, ElMessage } from 'element-plus'

import { businessList } from '@/api/business.js'
export default {
  data() {
    return {
      businessData: [],
      dialogFormVisibleEdit: false,
      businessEdit: {},
      title: '',
      searchBusiness: {},
      currentPage: 1,
      pageSize: 10,
      total: 400
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.selectBusiness();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.selectBusiness();
    },
    editBusiness() {

    },

    openEditDialog(row) {
      this.title = '修改商家信息'
      this.dialogFormVisibleEdit = true;
      this.businessEdit = JSON.parse(JSON.stringify(row));
    },

    selectBusiness() {
      businessList({
        params: {
          page: this.currentPage,
          pageSize: this.pageSize,
          contact: this.searchBusiness.contact,
          telephone: this.searchBusiness.telephone
        }
      })
        .then((result) => {
          //处理查询结果
          this.businessData = result.data.list;
          this.total = result.data.total;
        })
    },
    delEmployee(row) {
      ElMessageBox.confirm(
        '您确定要删除该员工吗?',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        //发起异步请求,完成删除功能

      }).catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
    }
  },

  created() {
    this.selectBusiness();
  }
}
</script>

<style scoped></style>